import axios from 'axios'
import { message } from 'antd'
import cookie from 'react-cookies'

const baseURL = process.env.REACT_APP_BASE_URL

const request = axios.create({
  timeout: 10000,
  baseURL: baseURL,
})


request.interceptors.request.use(config => {
  const token = cookie.load('token')
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, err => {
  return err
})

request.interceptors.response.use(
  res => {
    if(res.data.code === 401){
      cookie.remove('token')
      return
    }
    if (res.data.code !== 200) {
      message.open({
        content: res.data.msg || '系统错误',
        type: 'error'
      });

    } else {
      return res.data.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
